<template>
  <div class="cylinderFlowControl-TabErrorRecord">
    <aside-tree
      title="组织机构"
      :tree-data="treeData"
      :loading="$loadingPlugin.getTreeData"
      :mouse-button="false"
      @currentChange="currentChange"
      @refresh="getTreeData"
    />
    <div class="content">
      <el-table-form ref="form" :model="query">
        <el-form-item label="报警时间" prop="date">
          <el-date-picker
            v-model="query.date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item label="异常类型" prop="type">
          <el-select v-model="query.type" clearable placeholder="请选择">
            <el-option
              v-for="item in typeMap"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <template v-slot:btns>
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
        </template>
      </el-table-form>

      <!-- 表格内容 -->
      <lb-table
        ref="lbTable"
        v-el-height-adaptive-table="{ bottomOffset: 10 }"
        border
        highlight-current-row
        element-loading-text="加载中"
        :column="column"
        :data="tableData.data"
        pagination
        :total="tableData.total"
        custom-column
        @fetch="fetchData"
      />
    </div>

    <ErrorDetailDialog ref="ErrorDetailDialogRef" />
  </div>
</template>

<script>
  import getTreeData from '@/utils/jsonTree'
  import { mergeAsyncFunc } from '@/utils/mergeAsyncFunc'
  import { mapGetters } from 'vuex'
  import { queryErrorRecordList, queryMap } from '@/api'
  import AsideTree from '../../components/OrgTree'
  import dayjs from 'dayjs'
  import ErrorDetailDialog from './components/ErrorDetailDialog'
  export default {
    name: 'addition',
    components: {
      AsideTree,
      ErrorDetailDialog
    },
    data() {
      return {
        treeData: [],
        query: {
          date: [new Date(), new Date()],
          orgSNNo: '001000000000000000000',
          type: ''
        },
        tableData: {
          total: 0,
          data: []
        },
        typeMap: []
      }
    },
    computed: {
      ...mapGetters(['orgList']),
      column: function() {
        return [
          {
            label: '报警时间',
            prop: 'happendTime',
            width: 110,
            align: 'center',
            render: (h, { row }) => (
            <span>{row.happendTime && dayjs(row.happendTime).format('YY-MM-DD')}</span>
          )
          },
          { label: '责任机构', prop: 'orgName', width: 150, align: 'center' },
          { label: '扫瓶责任人', prop: 'ownerName', width: 150, align: 'center' },
          {
            label: '钢瓶编码',
            prop: 'gpbm',
            width: 150,
            align: 'center',
            render: (h, { row }) => {
              return (
              <el-link size='mini' underline={false} onClick={() => this.detali(1, row)}>
                {row.gpbm}
              </el-link>
              )
            }
          // formatter: this.formatter.gpbmFormat,
          },
          {
            label: '芯片编码',
            prop: 'xpbm',
            width: 150,
            align: 'center',
            render: (h, { row }) => {
              return (
              <el-link size='mini' underline={false} onClick={() => this.detali(2, row)}>
                {row.xpbm}
              </el-link>
              )
            }
          // formatter: this.formatter.xpbmFormat,
          },
          {
            label: '异常类型',
            prop: 'type',
            width: 150,
            align: 'center',
            render: (h, { row }) => <span>{this.typeFormat(row.type)}</span>
          // formatter: this.formatter.scanExceptionTypeFormat,
          }
        ]
      }
    },
    mounted() {
      this.getTreeData()
      this.queryOptionsMap()
    // scanExceptionTypeMap
    },
    methods: {
      // 获取树图数据
      async getTreeData() {
        const data = this.orgList.map((item) => ({
          label: item.orgName,
          ...item
        }))
        this.treeData = getTreeData(data)
      },
      // 选择变化
      currentChange(current) {
        this.query.orgSNNo = current.orgSNNo
        this.search()
      },
      // 查询枚举
      async queryOptionsMap() {
        const res = await mergeAsyncFunc(queryMap, {
          enumName: 'ScanExceptionType'
        })
        this.typeMap = res.data // 钢瓶流转管控状态
      },

      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          ...this._getQuery(this.query),
          pageIndex: currentPage,
          pageSize: pageSize
        }
        if (this.query.date && this.query.date.length > 0) {
          query[`happendTime`] = dayjs(this.query.date[0]).format()
          query[`happendTimeEnd`] = dayjs(this.query.date[1]).format()
        }
        delete query.date

        if (!this.query.type) query.type = -1

        const { status, data } = await queryErrorRecordList(query)
        if (status === 200) {
          this.tableData.data = data.rows
          if (currentPage === 1) this.tableData.total = data.total
        }
      },

      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      detali(type, row) {
        this.$refs['ErrorDetailDialogRef'].show(type, row)
      },
      typeFormat(val) {
        const index = this.typeMap.findIndex((v) => {
          return v.value === val
        })
        return index !== -1 ? this.typeMap[index].desc : ''
      }
    }
  }
</script>

<style lang="scss" scoped>
.cylinderFlowControl-TabErrorRecord {
  display: flex;
  // height: 630px;
  height: 100%;
  .content {
    flex: 1;
    width: 0;
    padding: 10px 10px 0 10px;
  }
}
</style>
